from flask import request, redirect
from blueprints import post_blueprint
from models import Blog, QueryOneInstance, DeleteInstance, UpdateInstance, Comment
from tools.decorate import pack_session
from tools.response import ResponsePostJson, Response


@post_blueprint.route('/my/model/delete/blog/<int:pid>', methods=['POST'])
@pack_session
def post_my_model_of_delete_blog(data):
    ins = QueryOneInstance(Blog,{'id':data['route']['pid']})
    DeleteInstance(ins)
    return redirect(request.referrer)

@post_blueprint.route('/my/model/comment', methods=['POST'])
@pack_session
def post_my_model_of_comment(data):
    def cb(json):
        ins = Comment(uid=data['session']['uid'],content=json['content'],pid=json['pid'])
        UpdateInstance(ins)
        return Response("评论成功")
    return ResponsePostJson(['content','pid'],cb)

@post_blueprint.route('/my/model/delete/comment/<int:cid>', methods=['POST'])
@pack_session
def post_my_model_of_delete_comment(data):
    ins = QueryOneInstance(Comment,{'id':data['route']['cid'],'uid':data['session']['uid']})
    DeleteInstance(ins)
    return redirect(request.referrer)

